package com.chen.blue.converter.business;

import com.chen.blue.domain.dto.business.ResourceDto;
import com.chen.blue.domain.dto.business.update.ResourceDtoEdit;
import com.chen.blue.domain.model.business.ResourceModel;
import com.chen.blue.domain.vo.business.ResourceVo;
import org.springframework.cglib.beans.BeanCopier;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created By CLT On 2024/5/24.
 */
@SuppressWarnings("all")
public abstract class ResourceConverter {

    private static final BeanCopier modelToVo = BeanCopier.create(ResourceModel.class, ResourceVo.class, false);
    private static final BeanCopier dtoToModel = BeanCopier.create(ResourceDto.class, ResourceModel.class, false);
    private static final BeanCopier dtoEditToModel = BeanCopier.create(ResourceDtoEdit.class, ResourceModel.class, false);

    /**
     * model->vo
     *
     * @param model
     * @return
     */
    public static ResourceVo modelToVo(ResourceModel model) {
        if (Objects.isNull(model)) {
            return null;
        }
        ResourceVo resourceVo = new ResourceVo();
        modelToVo.copy(model, resourceVo, null);
        return resourceVo;
    }

    /**
     * model->volist
     *
     * @param modelList
     * @return
     */
    public static List<ResourceVo> modelToVoList(List<ResourceModel> modelList) {
        if (modelList.size() == 0) {
            return null;
        }

        List<ResourceVo> resourceVoList = new ArrayList<>();
        for (ResourceModel resourceModel : modelList) {
            resourceVoList.add(modelToVo(resourceModel));
        }

        return resourceVoList;

    }

    /**
     * dto->model
     *
     * @param resourceDto
     * @return
     */
    public static ResourceModel dtoToModel(ResourceDto resourceDto) {

        if (Objects.isNull(resourceDto)) {
            return null;
        }

        ResourceModel resourceModel = new ResourceModel();
        dtoToModel.copy(resourceDto, resourceModel, null);
        return resourceModel;

    }

    /**
     * editDto->model
     *
     * @param resourceDtoEdit
     * @return
     */
    public static ResourceModel dtoEditToModel(ResourceDtoEdit resourceDtoEdit) {

        if (Objects.isNull(resourceDtoEdit)) {
            return null;
        }

        ResourceModel resourceModel = new ResourceModel();
        dtoEditToModel.copy(resourceDtoEdit, resourceModel, null);
        return resourceModel;

    }

}
